//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/data/dao/mongoDB/src/test/java/de/piratech/dasding/data/mongo/generator/CreateTestData.java $
//$Id: CreateTestData.java 94 2013-03-24 22:24:17Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.mongo.generator;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.ActionCategory;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.EventCategory;
import de.piratech.dasding.data.Location;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.DataType;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.UserData;
import de.piratech.dasding.data.database.mongo.MongoImpl;
import java.util.ArrayList;
import java.util.List;
import org.dummycreator.ClassBindings;
import org.dummycreator.DummyCreator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CreateTestData {

  @Autowired
  MongoImpl couch;
  List<EventCategory> eventCategories = new ArrayList<EventCategory>();
  List<ActionCategory> actionCategories = new ArrayList<ActionCategory>();

  @Test
  public void createTestData() throws DatabaseException {
    createActionCategories(couch);
    createEventCategories(couch);

    ActionData actionData = couch.getActionData();
    List<Action> actions = actionData.getActions();
    if (actions != null) {
      for (Action action : actions) {
        actionData.deleteAction(action);
      }
    }

    createEvents(couch);
  }

  private void createEventCategories(MongoImpl couch) throws DatabaseException {
    EventCategoryData eventCategoryData = couch.getEventCategoryData();
    List<EventCategory> categories = eventCategoryData.getCategories();
    for (EventCategory eventCategory : categories) {
      eventCategoryData.deleteEventCategory(eventCategory);
    }

    EventCategory event1 = new EventCategory();
    event1.setName("Demo");
    event1
        .setDescription("Etwas ist scheiße und diese Veranstaltung soll darauf aufmerksam machen.");
    event1.setVerified(true);

    EventCategory event2 = new EventCategory();
    event2.setName("Socializing");
    event2
        .setDescription("Ein Treffen bei dem man einfach nur eine schöne Zeit zusammen verbringt.");
    event2.setVerified(true);

    EventCategory event3 = new EventCategory();
    event3.setName("Barcamp");
    event3
        .setDescription("Treffen zu einem bestimmten Thema um darüber zu diskutieren und sich auszutauschen.");
    event3.setVerified(true);

    EventCategory event4 = new EventCategory();
    event4.setName("Was illegales");
    event4
        .setDescription("Das ist eine Eventkategory, die garantiert niemals freigeschalten und für alle zugänglich gemacht wird.");
    event4.setVerified(false);

    eventCategories.add(eventCategoryData.createEventCategory(event1));
    eventCategories.add(eventCategoryData.createEventCategory(event2));
    eventCategories.add(eventCategoryData.createEventCategory(event3));
    eventCategories.add(eventCategoryData.createEventCategory(event4));
  }

  private void createActionCategories(MongoImpl couch) throws DatabaseException {
    ActionCategoryData actionCategoryData = couch.getActionCategoryData();
    List<ActionCategory> categories = actionCategoryData.getCategories();
    for (ActionCategory actionCategory : categories) {
      actionCategoryData.deleteActionCategory(actionCategory);
    }

    ActionCategory cat1 = new ActionCategory();
    cat1.setName("Kochen");
    cat1.setDescription("Lebenmittel klein schnibbeln und in einen Topf tun. Den Topf auf eine heiße Herdplatte stellen und so lange umrühren bis alles weich ist");
    cat1.setVerified(true);

    ActionCategory cat2 = new ActionCategory();
    cat2.setName("Transportieren");
    cat2.setDescription("Etwas wahrscheinlich großes oder schweres von A nach B fahren.");
    cat2.setVerified(true);

    ActionCategory cat3 = new ActionCategory();
    cat3.setName("Flyer verteilen");
    cat3.setDescription("Alle Flyer verteilen. Egal an wen.");
    cat3.setVerified(true);

    ActionCategory cat4 = new ActionCategory();
    cat4.setName("Was illegales");
    cat4.setDescription("Das ist eine Actionkategory, die garantiert niemals freigeschalten und für alle zugänglich gemacht wird.");
    cat4.setVerified(false);

    actionCategories.add(actionCategoryData.createActionCategory(cat1));
    actionCategories.add(actionCategoryData.createActionCategory(cat2));
    actionCategories.add(actionCategoryData.createActionCategory(cat3));
    actionCategories.add(actionCategoryData.createActionCategory(cat4));

  }

  private List<Action> createActions(MongoImpl couch) throws DatabaseException {
    List<Action> newActions = new ArrayList<Action>();
    newActions.addAll(createActions(couch, actionCategories.get(0), 3));
    newActions.addAll(createActions(couch, actionCategories.get(1), 3));
    newActions.addAll(createActions(couch, actionCategories.get(2), 3));
    newActions.addAll(createActions(couch, actionCategories.get(3), 3));
    return newActions;
  }

  private List<Action> createActions(MongoImpl couch, ActionCategory category,
      int count) throws DatabaseException {
    List<Action> actions = new ArrayList<Action>();
    ActionData actionData = couch.getActionData();
    for (int i = 0; i < count; i++) {
      Action action = new DummyCreator(ClassBindings.defaultBindings())
          .create(Action.class);
      action.setCategory(category.getId());
      actions.add(actionData.createAction(action));
    }
    return actions;
  }

  private void createEvents(MongoImpl couch) throws DatabaseException {
    EventData eventData = couch.getEventData();
    List<Event> events = eventData.getEvents();
    for (Event event : events) {
      eventData.deleteEvent(event);
    }
    String[][] events1 = new String[][] { { "Event am Fernsehturm",
        "13.409286", "52.520739" } };
    createEvents(couch, events1, eventCategories.get(0), 3);
    String[][] events2 = new String[][] { { "Event am Hackeschen Markt",
        "13.402591", "52.523036" } };
    createEvents(couch, events2, eventCategories.get(1), 3);
    String[][] events3 = new String[][] { { "Event in den Rosenhöfen",
        "13.402269", "52.524460" } };
    createEvents(couch, events3, eventCategories.get(2), 3);
    String[][] events4 = new String[][] { { "Event am SEZ", "13.446000",
        "52.526679" } };
    createEvents(couch, events4, eventCategories.get(3), 3);

  }

  private void createEvents(MongoImpl couch, String[][] events,
      EventCategory category, int count) throws DatabaseException {
    EventData eventData = couch.getEventData();

    for (int i = 0; i < events.length; i++) {
      Event event = new DummyCreator(ClassBindings.defaultBindings())
          .create(Event.class);
      event.setActions(createActions(couch));
      event.setName(events[i][0]);
      event.setDataType(DataType.EVENT);
      event.setCategory(category.getId());
      Location location = new Location();
      location.setLongitude(new Double(events[i][1]));
      location.setLatitude(new Double(events[i][2]));
      event.setLocation(location);
      eventData.createEvent(event);
    }
  }

  private void createUsers(MongoImpl couch) throws DatabaseException {
    UserData userData = couch.getUserData();
    for (int i = 0; i < 10; i++) {
      User user = new DummyCreator(ClassBindings.defaultBindings())
          .create(User.class);
      user.setId(null);
      user.setRevision(null);
      user.setActionApplications(null);
      user.setInterestedIn(null);
      user.setDataType(DataType.USER);
      userData.createUser(user);
    }
  }
}
